package com.loan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loan.exeception.AppException;
import com.loan.mapper.ProductMapper;
import com.loan.entity.Product;
import com.loan.service.ProductService;
import com.loan.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author z
*/
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Override
    public List<Product> findAllProduct() {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Product::getUpdateTime);
        List<Product> products = productMapper.selectList(queryWrapper);
        log.info("products:{}",products);
        return products;
    }

    @Override
    public Product findProductById(String pid) {
        return productMapper.selectById(pid);
    }

    @Override
    public void insertProduct(Product product) {
        productMapper.insert(product);
    }

    @Override
    public ResponseResult<Product> deleteProduct(String pid) {
        int i = productMapper.deleteById(pid);
        if (i != 1){
            throw new AppException("删除产品失败");
        }
        return ResponseResult.success("删除产品成功");
    }
}




